package com.thingple.web.bridge.reader;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.thingple.eye.ActionResultListener;
import com.thingple.h5.bridge.AbstractJsBridge;
import com.thingple.h5.bridge.BridgeConfig;
import com.thingple.json.JsonConvertor;
import com.thingple.settings.PreferencesUtils;
import com.thingple.shared_context.SharedContext;
import com.thingple.uhf.RFIDDeviceManager;

import java.util.Map;

@BridgeConfig(moduleName = "reader")
public class ReaderJsBridgeImpl extends AbstractJsBridge implements ReaderJsBridge {

    private Activity activity;

    public ReaderJsBridgeImpl(WebView webView, Context context) {
        super(webView, context);
        this.activity = (Activity) context;
    }

    @JavascriptInterface
    @Override
    public void readCard() {
       readCard(null);
    }

    @JavascriptInterface
    @Override
    public void read(String params) {

        Bundle bundle = new Bundle();
        bundle.putInt("power", PreferencesUtils.getLowPower());

        Map json = JsonConvertor.convert2Object(params, Map.class);
        ReadCardListenerV2 listener = new ReadCardListenerV2(getWebView(), this);
        if (json != null) {
            Object filter = json.get("filter");
            if (filter != null) {
                bundle.putString("filter", filter.toString());
            }
            Object category = json.get("category");
            if (category != null) {
                bundle.putString("category", category.toString());
            }
            Object callback = json.get("callback");
            if (callback != null) {
                listener.setFuncName(callback.toString());
            }
        }

        RFIDDeviceManager.shareInstance().readCardForResponse(this.activity, listener, bundle);
    }

    @JavascriptInterface
    @Override
    public void readCard(String filter) {

        ActionResultListener listener = new ReadCardListener(getWebView(), this);
        Bundle bundle = null;
        if (filter != null) {
            bundle = new Bundle();
            int power = PreferencesUtils.getLowPower();
            bundle.putInt("power", power);
            bundle.putString("filter", filter);
        }
        RFIDDeviceManager.shareInstance().readCardForResponse(this.activity, listener, bundle);
    }

    @Override
    public void onReadCard(String tid, String epc, String user) {

    }

    @JavascriptInterface
    @Override
    public void writeCard(String filter, String epc, String password, String callbackFunc) {

        ActionResultListener listener = new WriteCardListener(getWebView(), this, callbackFunc);
        Bundle bundle = new Bundle();
        bundle.putString("filter", filter);
        if (epc != null) {
            bundle.putString("epc", epc);
        }
        if (password != null) {
            bundle.putString("password", password);
        }
        int power = PreferencesUtils.getHighPower();
        bundle.putInt("power", power);
        RFIDDeviceManager.shareInstance().writeCardForResponse(this.activity, listener, bundle);
    }

    @Override
    public void onWriteCard(int resultCode) {

    }

    @Override
    public void destroy() {
    }

}
